/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package services;

import beans.Fatura;
import beans.Usuario;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Date;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import static mb.ControllerFatura.md5Spring;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.springframework.util.DigestUtils;
import rn.FaturaRN;
import rn.UsuarioRN;
import util.HibernateUtil;
import util.Utilidades;

/**
 *
 * @author Caio
 */
@WebService(serviceName = "Posto")
public class Posto {
    
    private SessionFactory sf;
    
    public static String md5Spring(byte[] array){
        byte[] arquivo = DigestUtils.md5Digest(array);
        BigInteger bigInt = new BigInteger(1, arquivo);  
        String output = bigInt.toString(16);
        return output;
    }


    /**
     * Operação de Web service
     * @param bytea
     * @param data
     * @return 
     */
    @WebMethod(operationName = "salvarFatura")
    public Boolean salvarFatura(@WebParam(name = "bytea") byte[] bytea, 
            @WebParam(name = "data") Date data) {
        try {
            this.sf = HibernateUtil.getSessionFactory();
            this.sf.getCurrentSession().beginTransaction();
                                    
            
            FaturaRN rn = new FaturaRN();
            Fatura obj = new Fatura();

            byte[] zip = Utilidades.zipBytes(bytea);

            obj.setArquivo(zip);
            obj.setMd5(md5Spring(bytea));
            obj.setData_mes_referencia(data);

            rn.salvar(obj);
            
            this.sf.getCurrentSession().getTransaction().commit();

            return true;
        } catch (IOException e){
            if (this.sf.getCurrentSession().getTransaction().isActive()) {
                this.sf.getCurrentSession().getTransaction().rollback();
            }
            return false;
        } finally {
            this.sf.getCurrentSession().close();
        }
    }

    /**
     * Operação de Web service
     * @param bytea
     * @param data
     * @return 
     */
    @WebMethod(operationName = "salvarFaturaZip")
    public Boolean salvarFaturaZip(@WebParam(name = "arquivo") byte[] bytea, @WebParam(name = "data") Date data) {
        try {
            this.sf = HibernateUtil.getSessionFactory();
            this.sf.getCurrentSession().beginTransaction();
                                    
            
            FaturaRN rn = new FaturaRN();
            Fatura obj = new Fatura();

            byte[] zip = bytea;

            obj.setArquivo(zip);
            obj.setMd5(md5Spring(bytea));
            obj.setData_mes_referencia(data);

            rn.salvar(obj);
            
            this.sf.getCurrentSession().getTransaction().commit();

            return true;
        } catch (HibernateException e){
            if (this.sf.getCurrentSession().getTransaction().isActive()) {
                this.sf.getCurrentSession().getTransaction().rollback();
            }
            return false;
        } finally {
            this.sf.getCurrentSession().close();
        }
    }
    
}
